<?php
class sms_plugin_whatsnew{
	/**
	 * @param sms_controller_index $controller
	 */
	
	private static $obj;
	private static $new_sms_num;
	
	/**
	 * 初始化静态属性
	 *
	 * @param unknown_type $controller
	 */
	public static  function init($controller){
		if(!self::$obj){
			self::$obj =  Model::factory('sms_model_data');
		}
		if(!self::$new_sms_num){
			self::$new_sms_num = self::$obj->queryUnReadSms(Controller::$login['uid']);
		}
	}
	public static function getNewMessage($controller){
		self::init($controller);
		return $controller->newMessage = self::$new_sms_num['smsnum']>=0?self::$new_sms_num['smsnum']:0;
	}
	
	/**
	 * @param sms_controller_index $controller
	 */
	public static function getNewSystem($controller){
		self::init($controller);
		return $controller->newSystem = self::$new_sms_num['sysnum']>=0?self::$new_sms_num['sysnum']:0;
	}
	
	/**
	 * @param sms_controller_index $controller
	 */
	public static function getNewAnnouncement($controller){
		$o = Model::factory("sms_model_annlog");
		return $controller->newAnnouncement = $o->getUnread(Controller::$login['uid'], 0);
	}
	
	public static function setReadMessage($controller){
		self::init($controller);
		$affected = self::$obj->cleanSmsCount(Controller::$login['uid']);
		self::$obj->setReaded(Controller::$login['uid'],true);
		if($affected){
			CacheManager::getInstance()->refresh('sms');
		}
		return ;
	}
	
	public static function setReadSystem($controller){
		self::init($controller);
		$affected = self::$obj->cleanSysSmsCount(Controller::$login['uid']);
		self::$obj->setReaded(Controller::$login['uid'],false);
		CacheManager::getInstance()->refresh('sms');
		if($affected>0){
			CacheManager::getInstance()->refresh('sms');
		}
	}
	
	public static function setReadAnnouncement($controller){
		$uid = Controller::$login['uid'];
		$o = Model::factory("sms_model_annlog");
		$unreads = $o->getUnreadIds($uid);
		$a = array();
		foreach($unreads as $unread){
			array_push($a, array('uid'=>$uid, 'ann_id'=>$unread['id']));
		}
		if(count($a)>0){
			$o->add($a);
			CacheManager::getInstance()->refresh('sms');
		}
	}
	/**
	 * 更新未读短消息数量
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $type
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function setNewMsgCount($uid,$type,$num=1){
		if(!($o=self::$obj)){
			$o = Model::factory('sms_model_data');
		}
		return $o->updateSmsCount($uid,$type,$num);
	}
	
	/**
	 * 更新评论未读短消息数
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function setNewCommenCount($uid,$num=1){
		return self::setNewMsgCount($uid,sms_model_data::MSG_COMMENT_CNT,$num);
	}
	/**
	 * 更新评论未读短消息数
	 *
	 * @param unknown_type $uid
	 * @param unknown_type $num
	 * @return unknown
	 */
	public static function resetNewCommenCount($uid){
		return self::setNewMsgCount($uid,sms_model_data::MSG_COMMENT_CNT,0);
	}
}